//
//  ITStatusBarViewController.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/8/11.
//  Copyright © 2020 魏延龙. All rights reserved.
//  都不需要修改info.plist文件

#import "ITStatusBarViewController.h"
#import "AppDelegate.h"

@interface ITStatusBarViewController ()

@property (nonatomic, strong) UISegmentedControl *seg1;
@property (nonatomic, strong) UISegmentedControl *seg2;
@property (nonatomic, strong) UISegmentedControl *seg3;

@property (nonatomic, strong) UISegmentedControl *seg4;

@property (nonatomic, strong) UISegmentedControl *seg5;

@property (nonatomic, assign) UIStatusBarStyle statusBarStyle;
@property (nonatomic, assign) BOOL hiddenStatusBar;

@end

@implementation ITStatusBarViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];

    self.statusBarStyle = UIStatusBarStyleDefault;
    self.hiddenStatusBar = NO;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"leftBackButton"] style:UIBarButtonItemStylePlain target:self action:@selector(backToPrevious)];
    self.title = @"leftBackButton";
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    
    [self setupViews];
    
}

-(void)backToPrevious {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = nil;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor]};
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor]};
}



-(void)viewWillLayoutSubviews {
    
    self.seg1.frame = CGRectMake((self.view.frame.size.width - 150) / 2, 160, 150, 44);
    self.seg2.frame = CGRectMake((self.view.frame.size.width - 150) / 2, 210, 150, 44);
    self.seg3.frame = CGRectMake((self.view.frame.size.width - 150) / 2, 260, 150, 44);
    self.seg4.frame = CGRectMake((self.view.frame.size.width - 150) / 2, 350, 150, 44);
    self.seg5.frame = CGRectMake((self.view.frame.size.width - 150) / 2, 440, 150, 44);
}

-(void)setupViews {
        
    self.seg1 = [[UISegmentedControl alloc] initWithItems:@[@"显示", @"隐藏"]];
    [self.view addSubview:self.seg1];
    [self.seg1 addTarget:self action:@selector(segment1Select:) forControlEvents:UIControlEventValueChanged];
    [self.seg1 setSelectedSegmentIndex:0];
    
    self.seg2 = [[UISegmentedControl alloc] initWithItems:@[@"深色", @"浅色"]];
    [self.view addSubview:self.seg2];
    [self.seg2 addTarget:self action:@selector(segment2Select:) forControlEvents:UIControlEventValueChanged];
    [self.seg2 setSelectedSegmentIndex:0];
    
    self.seg3 = [[UISegmentedControl alloc] initWithItems:@[@"深色", @"浅色"]];
    [self.view addSubview:self.seg3];
    [self.seg3 addTarget:self action:@selector(segment3Select:) forControlEvents:UIControlEventValueChanged];
    [self.seg3 setSelectedSegmentIndex:0];
    
    self.seg4 = [[UISegmentedControl alloc] initWithItems:@[@"透明", @"蓝色"]];
    [self.view addSubview:self.seg4];
    [self.seg4 addTarget:self action:@selector(segment4Select:) forControlEvents:UIControlEventValueChanged];
    [self.seg4 setSelectedSegmentIndex:0];
    
    self.seg5 = [[UISegmentedControl alloc] initWithItems:@[@"透明", @"蓝色"]];
    [self.view addSubview:self.seg5];
    [self.seg5 addTarget:self action:@selector(segment5Select:) forControlEvents:UIControlEventValueChanged];
    [self.seg5 setSelectedSegmentIndex:0];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 100, 100)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    
}


-(void)segment1Select:(UISegmentedControl*)seg{
    //@"显示", @"隐藏"
    if (seg.selectedSegmentIndex == 0) {
        self.hiddenStatusBar = NO;
    } else {
        self.hiddenStatusBar = YES;
    }
    [self setNeedsStatusBarAppearanceUpdate];
}

-(void)segment2Select:(UISegmentedControl*)seg{
    //@"深色", @"浅色"
    if (seg.selectedSegmentIndex == 0) {
        self.statusBarStyle = UIStatusBarStyleDefault;
    } else {
        self.statusBarStyle = UIStatusBarStyleLightContent;
    }
    [self setNeedsStatusBarAppearanceUpdate];
}

-(void)segment3Select:(UISegmentedControl*)seg{
    //@"深色", @"浅色"
    // 这种方式vc 和 navc 不能重写 preferredStatusBarStyle 方法
    // 全局搜索 preferredStatusBarStyle 注释掉可以测试
    if (seg.selectedSegmentIndex == 0) {
        self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    } else {
        self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    }
}

-(void)segment4Select:(UISegmentedControl*)seg{
    if (seg.selectedSegmentIndex == 0) {
        [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    } else {
//        [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:0.5 green:0.9 blue:0.5 alpha:0.5]] forBarMetrics:UIBarMetricsDefault];
//        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
//        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
        
        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:nil];
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor blackColor]};
    }
}

-(void)segment5Select:(UISegmentedControl*)seg{
    if (seg.selectedSegmentIndex == 0) {
        self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor redColor]}];
    } else {
        self.navigationItem.leftBarButtonItem.tintColor = [UIColor redColor];
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor blueColor]}];
    }
}

-(UIStatusBarStyle)preferredStatusBarStyle {
    // 如果是单个VC 可以只在控制器中重写，如果VC包在NVC里面 NVC也要重写
    return self.statusBarStyle;
}

- (BOOL)prefersStatusBarHidden {
    return self.hiddenStatusBar;
}

- (UIImage *)imageWithColor:(UIColor *)color {

    //创建1像素区域并开始图片绘图
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    
    //创建画板并填充颜色和区域
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    //从画板上获取图片并关闭图片绘图
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
